package com.rin.design_pattern.singleton;

/**
 * @author RinMi
 * @since 2023/5/26
 */
public class LazySingleton {
    private static LazySingleton lazySingleton;
//    private volatile static LazySingleton lazySingleton;

    private String message;

    private static class LazySingletonHolder{
        private static final LazySingleton LAZY_SINGLETON = new LazySingleton();
    }

    private LazySingleton() {
    }

    public static LazySingleton newInstance() {
        if (lazySingleton == null) {
            lazySingleton = new LazySingleton();
        }
        System.out.println(lazySingleton);
        return lazySingleton;
    }

    public static synchronized LazySingleton safeNewInstance() {
        if (lazySingleton == null) {
            lazySingleton = new LazySingleton();
        }
        System.out.println(lazySingleton);
        return lazySingleton;
    }

    public static LazySingleton doubleLockNewInstance() {
        if (lazySingleton == null) {
            synchronized (LazySingleton.class) {
                if (lazySingleton == null) {
                    lazySingleton = new LazySingleton();
                }
            }
        }
        System.out.println(lazySingleton);
        return lazySingleton;
    }

    public static LazySingleton getInstance(){
        System.out.println(LazySingletonHolder.LAZY_SINGLETON);
        return LazySingletonHolder.LAZY_SINGLETON;
    }

    public void showMessage(String message) {
        setMessage(message);
        System.out.println(this.message);
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
